package com.hechi.demo.controller;

import com.hechi.demo.dto.OrdersListRequestDto;
import com.hechi.demo.dto.ResultResponse;
import com.hechi.demo.entity.Doctor;
import com.hechi.demo.exception.TijianException;
import com.hechi.demo.exception.TijianExceptionEnum;
import com.hechi.demo.service.OrdersServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 订单模块的控制器
 */
@RestController
public class OrdersController {

    @Autowired
    OrdersServiceImpl ordersService;

    @GetMapping("/order/{orderId}")
    public ResultResponse getOrderInfo(HttpServletRequest request,@PathVariable("orderId") Integer orderId){
        // System.out.println("orderId:"+orderId);
        //校验医生有没有登录
        Doctor doctor= (Doctor) request.getAttribute("doctor");
        if(doctor==null){
            throw new TijianException(TijianExceptionEnum.DOCTOR_LOGIN_TIMEOUT.getEnCode(),TijianExceptionEnum.DOCTOR_LOGIN_TIMEOUT.getEnMessage());
        }
        //校验通过之后该干嘛干嘛---->调用service查询数据：三张表orders users setmeal

        return ordersService.getOrderInfo(orderId);
    }


    @RequestMapping("/page")
    public ResultResponse page(@RequestBody OrdersListRequestDto ordersListRequestDto, HttpServletRequest request){                 //json-->java
        ResultResponse resultResponse=null;
        /*
        1.访问当前接口需要校验身份
        2.根据请求参数调用service获取最终的结果-----请求参数需要非空校验
        3.调用service，该干嘛干嘛（分页功能）
         */

        //1.访问当前接口需要校验身份
        Doctor doctor= (Doctor) request.getAttribute("doctor");
        if(doctor==null){
            //直接抛出异常是可以的  throw new TijianException();
            //这个没有抛出异常
            resultResponse=new ResultResponse(TijianExceptionEnum.DOCTOR_LOGIN_TIMEOUT.getEnCode(),TijianExceptionEnum.DOCTOR_LOGIN_TIMEOUT.getEnMessage(),null);
        }else{
           //偷懒，不进行非空校验
            resultResponse=ordersService.page(ordersListRequestDto);
        }

        return resultResponse;
    }

}
